import {request} from "../../request/index.js"
Page({
  data:{
    inputVal:'',
    nickName:'',
    avatarUrl:'',
    info:''
  },
  onLoad(){
    // 得到当前用户头像、昵称
    let that = this;
    wx.getStorage({
      key: 'nickName',
      success: function(res){
        that.setData({
          nickName:res.data
        })
      }
    });
    wx.getStorage({
      key: 'avatarUrl',
      success: function(res){
        that.setData({
          avatarUrl:res.data
        })
      }
    });
  },
  async formSubmit(e){
    let that = this;
    // 如果发表内容为空 则不发布且弹窗提示用户
    if(e.detail.value.textarea.length==0){
      wx.showToast({
        title: '发布内容不能为空',
        icon: 'none',
        mask:true,
      })
    }else{
      
      that.setData({
        inputVal:e.detail.value.textarea
      })
      let nickName=that.data.nickName;
      let avatarUrl=that.data.avatarUrl;
      let inputVal=that.data.inputVal;
      // 发请求将用户发表内容存入数据库
      const forumCon={nickName,avatarUrl,inputVal};
      // console.log(forumCon);
      const addForum = await request({url:"/addForum",method:'Post',data:forumCon});
      // console.log(addForum);
      
      // 得到请求结果  成功则弹窗提示
      if(addForum.data.message=='发表成功'){
        wx.showToast({
          title: '发表成功',
          icon: 'success',
          mask:true,
          success:function(){
            // 提交成功清空输入框内容
            that.setData({
              info:''
            })
          }
        })
      }
    }
    
    
    
  }
})